<template>
    <div>
        <el-drawer
          :title="sunhaoId > 0 ? '编辑' : '新增'"
          :visible.sync="drawer"
          direction="rtl"
          :show-close="true"
          :wrapper-closable="true"
          :append-to-body="true"
          :wrapperClosable="false"
          :before-close="handleClose"
          :size="`35%`"
        >
            <div style="width:90%;margin-left:6%;">
               <sunhaoForm ref="sunhaoform" v-if="drawer" :id="rowObj.id" :rowObj="rowObj" :status="status" :typeData="typeData" :statusData="statusData" :sunhaoData="sunhaoData" @handlersunhaoSubmit="handlersunhaoSubmit"></sunhaoForm>
            </div>
        </el-drawer>
    </div>
</template>
<script>
    import sunhaoForm from './form'
    import { crudModuleListApi, crudModuleSaveDataApi, crudModuleUpdateApi } from '@/api/develop'
    import { getDictDataListApi } from '@/api/form'
    export default {
        components: { sunhaoForm },
        data () {
            return {
                drawer: false,
                sunhaoId: 0,
                sunhaoData:[],
                total: 0,
                pagesearch: {
                    page: 1,
                    limit: 100,
                    order_by: {},
                    scope_frame: 'all',
                    show_search_type: '0',
                    view_search: [],
                    view_search_boolean: ''
                },
                rowObj: {},
                typeData: [],
                statusData: [],
                dictType: {
                    types: 'sunhaoleixing',
                    level: 1,
                    pid: ''
                },
                dictStatus: {
                    types: 'zhuangtai',
                    level: 1,
                    pid: ''
                }
            }
        },
        props: {
            status: {
                type: Number,
                default: ()=>{
                    return 0
                }
            }
        },
        mounted() {
            
        },
        methods: {
            async openBox(id=0, data){   
                console.log("data", data)          
                if(this.status==1){
                    this.rowObj = data;
                    this.handlerSunList(data);
                }
                let resdata1 = await getDictDataListApi(this.dictType);
                this.typeData = resdata1.data.list;
                let resdata = await getDictDataListApi(this.dictStatus)
                this.statusData = resdata.data.list;
                if(resdata){
                    this.drawer = true;
                    if(id && this.status==2){
                        this.rowObj = data;
                    }
                }
            },
            handlersunhaoSubmit(data, index){
                this.drawer = false;
                if(index==1){
                    this.savaData(data)
                }else if(index==2){
                    this.upladData(this.rowObj.id, data)
                }else if(index==3){
                    this.$emit("handlersunhaoSubmit", data)
                }
            },
            handleClose(){
                this.drawer = false;
            },
            async handlerSunList(row){
                crudModuleListApi("sunhaoshezhi", this.pagesearch).then(res => {
                    console.log("损耗：",res)
                    if(res.status==200){
                        this.sunhaoData = res.data.list;
                        this.total = res.data.count;
                        if(this.status==1){
                           
                           
                        }
                    }
                })
            },
            async handlerTypeList(searchdata, index=1){
                console.log("2", index)
                getDictDataListApi(searchdata).then(res=>{
                    if(res.status==200){
                        if(index==1){
                            this.typeData = res.data.list;
                            console.log("A", this.typeData)
                        }else if(index==2){
                            this.statusData = res.data.list;
                        }
                    }
                })
            },
            // 实体列表新增保存
            savaData(data) {
                // 原代码
                crudModuleSaveDataApi('sunhaoshezhi', data)
                .then((res) => {
                if (res.status == 200) {
                    this.handleClose()
                    this.$emit('getList')
                }
            
                })
                .catch((err) => {
                
                })
            },
            // 实体列表更新保存
            upladData(id, data) {
               // 原代码
                crudModuleUpdateApi('sunhaoshezhi', id, data)
                .then((res) => {
                if (res.status == 200) {
                    this.handleClose()
                    this.$emit('getList')
                
                }
                
                })
                .catch((err) => {
            
                })
            
            },
    
            
        }
 
    }

</script>
<style scoped>

/deep/ .el-form-item__label{
    line-height: 28px;
    padding-bottom:5px;
}
/deep/ .el-form-item{
    margin-bottom:10px;
}

</style>